package com.jiuyin.dianjing.ui.activity.match;

import android.util.Pair;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.TabAdapter;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.MatchGameItemModel;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.fragment.match.FragmentMatch;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_MATCH_TYPE_MY;

/**
 * 我要参赛
 */
public class MatchEnterActivity extends BaseActivity {
    protected final List<Pair<String, Fragment>> mFragments = new ArrayList<>();
    @BindView(R.id.vp_view_container)
    ViewPager mViewPager;
    @BindView(R.id.tabs)
    TabLayout mTabLayout;
    private TabAdapter mTabAdapter;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_match_enter;
    }

    @Override
    protected void initData() {
        addData();
    }

    @Override
    protected void initView() {
        setTitle(R.string.match_enter);
    }

    protected void addData() {
        getGameList();
    }

    protected void addFragments(List<MatchGameItemModel> lists) {
        for (MatchGameItemModel model : lists) {
            mFragments.add(Pair.create(model.getName(), FragmentMatch.newInstance(model.getGameId(),false)));
        }
    }

    protected void initTabLayout() {
        mTabAdapter = new TabAdapter(getSupportFragmentManager(), mFragments);
        mViewPager.setAdapter(mTabAdapter);
        mViewPager.setOffscreenPageLimit(mFragments.size());
        mTabLayout.setupWithViewPager(mViewPager);
    }

    /**
     * 获取游戏列表
     */
    private void getGameList() {
        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_TOKEN, HelperApplication.getToken());
        params.put(KEY_TYPE, VALUE_MATCH_TYPE_MY);
        JSONObject jsonObject = new JSONObject(params);
        getApiHelper().appGetMyCompetitionGameList(jsonObject.toString(), data -> {
            addFragments(data);
            initTabLayout();
        });
    }
}
